package sctu.edu.java.year2016.team03.huangbaolian.exiercise.d20160602;

//import sctu.edu.java.year2016.team06.yuweimei.day20160529.chapter8_2.Color;

import com.sun.org.apache.xerces.internal.dom.PSVIAttrNSImpl;

import javax.swing.*;
import java.awt.*;
import java.util.Random;

/**
 * Created by hbl on 2016/6/6.
 */
public class SleepMethod1 extends JFrame {
    private Thread t;
    private static Color[] color = { Color.blue, Color.CYAN, Color.GREEN, Color.ORANGE, Color.RED, Color.PINK,Color.blue, Color.CYAN, Color.GREEN, Color.ORANGE, Color.RED, Color.PINK,Color.blue, Color.CYAN, Color.GREEN, Color.ORANGE, Color.RED, Color.PINK,Color.blue, Color.CYAN, Color.GREEN, Color.ORANGE, Color.RED, Color.PINK,Color.blue, Color.CYAN, Color.GREEN, Color.ORANGE, Color.RED, Color.PINK};
    private static final Random rand = new Random();
    private static Color getC(){
        return color[rand.nextInt(color.length)];
    }
    public SleepMethod1(){
        t = new Thread(new Runnable() {
            int x = 300;
            int y = 500;
            @Override
            public void run() {
                while(true){
                    try{
                        t.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Graphics graphics = getGraphics();
                    graphics.setColor(getC());
                    graphics.drawLine(x,y,400,y++);
                    if(y>=80){
                        y=50;
                    }
                }
            }
        });
        t.start();
    }

    public static void main(String[] args) {
        init(new SleepMethod1(), 100, 100);
    }
    public static void init(JFrame frame,int width,int height){
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(width, height);
        frame.setVisible(true);
    }
}
